<?php
/**
 * Abstract model for a build log.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Abstract model for a build log.
 *
 * @package PhpCilux
 */
abstract class Model_Log_Abstract
{

    /**
     * Holds the build dir.
     *
     * @var string
     */
    protected $_buildDir;

    /**
     * Holds an instance of the class.
     *
     * @var array
     */
    protected $_projects;

    /**
     * The build key.
     *
     * @var string
     */
    protected $_buildKey;

    /**
     * The name of the logFile.
     *
     * @var string
     */
    protected $_logFilename = '';

    /**
     * Constructor.
     *
     * @param string $projectKey The project key.
     * @param string $buildKey   The build key.
     */
    public function __construct($projectKey, $buildKey)
    {
        $this->_projectKey = $projectKey;
        $this->_buildKey   = $buildKey;

        $this->_buildDir = APPLICATION_PATH . DIRECTORY_SEPARATOR .
                           '..' . DIRECTORY_SEPARATOR .
                           'data' . DIRECTORY_SEPARATOR .
                           'build' . DIRECTORY_SEPARATOR .
                            $this->_projectKey . DIRECTORY_SEPARATOR .
                           'reports' . DIRECTORY_SEPARATOR .
                            $this->_buildKey . DIRECTORY_SEPARATOR;

        $this->_buildDir = realpath($this->_buildDir) . DIRECTORY_SEPARATOR;
    }

    /**
     * Check for existing the log-file of the log-type.
     *
     * @return boolean
     */
    public function exists()
    {
        $fileName = $this->_getLogFileName();

        return is_file($fileName);
    }

    /**
     * Get the log content for the PHP Codesniffer log.
     *
     * @return string
     */
    public function getContent()
    {
        $fileName = $this->_getLogFileName();

        return file_get_contents($fileName);
    }

    /**
     * Get the status of the PHP Codesniffer log.
     *
     * @return string
     */
    abstract public function status();

    /**
     * Formating the log.
     *
     * @param string $content The log content which will be styled.
     *
     * @return string
     */
    abstract public function format($content);

    /**
     * Get the log filename.
     *
     * @return string
     * @throws Exception Missing log file name.
     */
    protected function _getLogFileName()
    {
        if (empty($this->_logFilename)) {
            throw new Exception('Missing log file name.');
        }

        $fileName = $this->_buildDir .
                    $this->_logFilename;

        return $fileName;
    }

}